﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcDatagrid.Models;

namespace MvcDatagrid.Controllers
{
    public class EmployeesController : Controller
    {
        private IEmployeesRepository _employeesRepository;


        public EmployeesController(IEmployeesRepository employeesRepository)
        {
            _employeesRepository = employeesRepository;
        }

        public ActionResult Index()
        {
            var employees = _employeesRepository.GetAllEmployees();
                //.Include("Employees2");
            return View(employees.ToList());
        }

        //
        // GET: /Employees/Details/5

        public ActionResult Details(int id = 0)
        {
            Employees employees = _employeesRepository.GetEmployeeById(id);
            if (employees == null)
            {
                return HttpNotFound();
            }
            return View(employees);
        }

        //
        // GET: /Employees/Create

        public ActionResult Create()
        {
            ViewBag.ReportsTo = new SelectList(_employeesRepository.GetAllEmployees(), "EmployeeID", "LastName");
            return View();
        }

        //
        // POST: /Employees/Create

        [HttpPost]
        public ActionResult Create(Employees employees)
        {
            if (ModelState.IsValid)
            {
                _employeesRepository.AddEmployee(employees);
                return RedirectToAction("Index");
            }

            ViewBag.ReportsTo = new SelectList(_employeesRepository.GetAllEmployees(), "EmployeeID", "LastName", employees.ReportsTo);
            return View(employees);
        }

        //
        // GET: /Employees/Edit/5

        public ActionResult Edit(int id = 0)
        {
            Employees employees = _employeesRepository.GetEmployeeById(id);
            if (employees == null)
            {
                return HttpNotFound();
            }
            ViewBag.ReportsTo = new SelectList(_employeesRepository.GetAllEmployees(), "EmployeeID", "LastName", employees.ReportsTo);
            return View(employees);
        }

        //
        // POST: /Employees/Edit/5

        [HttpPost]
        public ActionResult Edit(Employees employees)
        {
            if (ModelState.IsValid)
            {
                _employeesRepository.UpdateEmployees(employees);
                return RedirectToAction("Index");
            }
            ViewBag.ReportsTo = new SelectList(_employeesRepository.GetAllEmployees(), "EmployeeID", "LastName", employees.ReportsTo);
            return View(employees);
        }

        //
        // GET: /Employees/Delete/5

        public ActionResult Delete(int id = 0)
        {
            Employees employees = _employeesRepository.GetEmployeeById(id);
            if (employees == null)
            {
                return HttpNotFound();
            }
            return View(employees);
        }

        //
        // POST: /Employees/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Employees employees = _employeesRepository.GetEmployeeById(id);
            _employeesRepository.DeleteEmployees(employees);
            return RedirectToAction("Index");
        }

        //protected override void Dispose(bool disposing)
        //{
        //    db.Dispose();
        //    base.Dispose(disposing);
        //}
    }
}